
package com.application.chiettinhdienluc.db;

import java.util.ArrayList;

import android.content.Context;
import android.database.Cursor;

import com.application.chiettinhdienluc.model.DonViQuanLyModel;

public class DonViQuanLyDataSource {
    // Database fields
    private final MySQLiteHelper dbHelper;

    private final static String TAG = DonViQuanLyDataSource.class.getSimpleName();

    public DonViQuanLyDataSource(Context context) {
        dbHelper = MySQLiteHelper.getInstance();
    }

    public DonViQuanLyModel getDonViByMaDonVi(String maDonVi) {
		
    	DonViQuanLyModel donvi = null;
		Cursor c = null;
		
		try {
			if(dbHelper != null && dbHelper.getDb() != null)
			{
				c = dbHelper.getDb().rawQuery("select * from " + DonViQuanLyModel.TABLE_NAME + " where MA_DVIQLY = '" + maDonVi + "'", null);

				if (c != null && c.getCount() > 0) {
					int colId_MaDonVi = c.getColumnIndex(DonViQuanLyModel.FULL_PROJECTION[0]);
					int colId_TenDonVi= c.getColumnIndex(DonViQuanLyModel.FULL_PROJECTION[1]);
					int colId_KhuVuc = c.getColumnIndex(DonViQuanLyModel.FULL_PROJECTION[2]);			

					if (c.moveToFirst()) {
					
						try {
							String madonvi = c.getString(colId_MaDonVi);
							String ten = c.getString(colId_TenDonVi);
							String khuvuc = c.getString(colId_KhuVuc);					

							donvi = new DonViQuanLyModel();

							donvi.setMaDonVi(madonvi);
							donvi.setTenDonVi(ten);
							donvi.setKhuVuc(khuvuc);	
							
						} catch (Exception e){
							e.printStackTrace();
						}
					}		
				}
			}
		} catch (Exception e) {
		}
		finally{
			if(c!=null)
			{
				c.close();
				c = null;
			}
		}

		return donvi;
	}
    
    public ArrayList<DonViQuanLyModel> loadLoaiHoSo() {
		ArrayList<DonViQuanLyModel> listDonVi = new ArrayList<DonViQuanLyModel>();
		Cursor c = null;
		
		try {
			if(dbHelper != null && dbHelper.getDb() != null)
			{
				c = dbHelper.getDb().rawQuery("select * from " + DonViQuanLyModel.TABLE_NAME, null);

				if (c != null && c.getCount() > 0) {
					int colId_MaDonVi = c.getColumnIndex(DonViQuanLyModel.FULL_PROJECTION[0]);
					int colId_TenDonVi= c.getColumnIndex(DonViQuanLyModel.FULL_PROJECTION[1]);
					int colId_KhuVuc = c.getColumnIndex(DonViQuanLyModel.FULL_PROJECTION[2]);			

					c.moveToFirst();
					do {
						try {
							String maDonVi = c.getString(colId_MaDonVi);
							String ten = c.getString(colId_TenDonVi);
							String khuvuc = c.getString(colId_KhuVuc);					

							DonViQuanLyModel lhs = new DonViQuanLyModel();

							lhs.setMaDonVi(maDonVi);
							lhs.setTenDonVi(ten);
							lhs.setKhuVuc(khuvuc);					

							listDonVi.add(lhs);
						} catch (Exception e) {
						}
					} while (c.moveToNext());
				}
			}
		} catch (Exception e) {
		}
		finally{
			if(c!=null)
			{
				c.close();
				c = null;
			}
		}

		return listDonVi;
	}
}
